<?php
namespace App\Admin\Controllers\Products;

use App\Admin\Extensions\Grid\Actions\BatchRestore;
use App\Admin\Extensions\Grid\Actions\Restore;
use App\Exporter\Products\SkuTrashedExporter;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
class SkuTrashedController extends AdminController{

    protected $title = '产品回收站';

    protected function grid()
    {
        $grid = new Grid(new ProductSku());
        $grid->model()->newQuery()
            ->orderBy('id', 'DESC')
            ->onlyTrashed();
        $grid->column('id','ID')->filter();
        $grid->column('psku_code','SKU');
        $grid->column('part_number','PartNo.');
        $grid->column('is_mix','组合')->using(ProductSku::$_isMix)->filter(ProductSku::$_isMix)->label([1 => 'info', 0 => 'default']);
        $grid->column('mix_rule','组合规则')->style('max-width:200px');
        $grid->column('is_sales','是否在售')->bool()->filter(ProductSku::$_isSales);
        $grid->column('psku_cn','名称')->style('max-width:200px');
        $grid->column('classify.season','季节性')->using(ProductClassify::$_season);
        $grid->column('classify.title','品类')->style('max-width:200px');
        $grid->column('img','图片')->image('',60,60);
        $grid->column('psku_status','产品状态')->using(ProductSku::$_pSkuStatus)->filter(ProductSku::$_pSkuStatus);
        $grid->column('muser_id','负责人')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('cuser_id','创建人')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->exporter(new SkuTrashedExporter());
        $grid->disableCreateButton();
        $grid->fixHeader();

        $grid->disableActions();

        if(Admin::user()->can('sku-trashed.restore')){
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                $batch->disableDelete();
                $batch->add(new BatchRestore());
            });
        }else{
            $grid->disableBatchActions();
        }

        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->where(function ($query) {
                    $query->where('psku_code', 'like', "%{$this->input}%")
                        ->orWhere('psku_cn', 'like', "%{$this->input}%")
                        ->orWhere('part_number', 'like', "%{$this->input}%");
                }, '查询')->placeholder('模糊查询SKU，名称，PartNo.');
                $filter->equal('muser_id','负责人')->select(User::getMUserOptions());
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('classify.season', '季节性')->select(ProductClassify::$_season);
                $filter->equal('classify_id', '品类')->select(ProductClassify::options());
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('cuser_id','创建人')->select(User::getCUserOptions());
            });
        });

        return $grid;
    }
}
